using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Matrix.Configuration.Constants;

namespace Matrix.Configuration.Configurations
{
    /// <summary>
    /// ConfigurationItem实体配置
    /// </summary>
    public class ConfigurationItemConfiguration : IEntityTypeConfiguration<ConfigurationItem>
    {
        public void Configure(EntityTypeBuilder<ConfigurationItem> builder)
        {
            builder.ToTable("ConfigurationItems");

            // 配置主键
            builder.HasKey(x => x.Id);

            // 配置索引
            builder.HasIndex(x => new { x.TenantId, x.Namespace, x.Key })
                .IsUnique()
                .HasDatabaseName("IX_ConfigurationItems_TenantId_Namespace_Key");

            builder.HasIndex(x => x.Namespace)
                .HasDatabaseName("IX_ConfigurationItems_Namespace");

            builder.HasIndex(x => x.Key)
                .HasDatabaseName("IX_ConfigurationItems_Key");

            builder.HasIndex(x => x.Version)
                .HasDatabaseName("IX_ConfigurationItems_Version");

            // 配置属性
            builder.Property(x => x.Namespace)
                .IsRequired()
                .HasMaxLength(ConfigurationItemConsts.MaxNamespaceLength);

            builder.Property(x => x.Key)
                .IsRequired()
                .HasMaxLength(ConfigurationItemConsts.MaxKeyLength);

            builder.Property(x => x.Value)
                .IsRequired()
                .HasMaxLength(ConfigurationItemConsts.MaxValueLength);

            builder.Property(x => x.DataType)
                .IsRequired()
                .HasMaxLength(ConfigurationItemConsts.MaxDataTypeLength);

            builder.Property(x => x.Description)
                .HasMaxLength(ConfigurationItemConsts.MaxDescriptionLength);

            builder.Property(x => x.Version)
                .IsRequired()
                .HasDefaultValue(1);

            // 配置租户过滤
            builder.ConfigureByConvention();

            // 配置关系 - ExtraProperties是ABP框架自动处理的
            // 无需手动配置
        }
    }
}